package com.hcb.tonguetiphealth.manager;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;

import com.hcb.tonguetiphealth.global.Global;
import com.hcb.tonguetiphealth.model.Chat;
import com.hcb.tonguetiphealth.model.User;

public class ChatManager {
	private final int HISTORYCHATS_NUM = 10;
	private String tableName;
	private String TABLE_CHAT_SQL;
	private DBManager db;
	
	public ChatManager(Activity ctx) {
		db = DBManager.getInstance(ctx);
	}
	
	/**
	 * 保存聊天记录
	 * @param user
	 * @param chartList
	 */
	public void saveChatList(List<Chat> chartList) {
		tableName = "chat_" + Global.user.getUserId() + "_" + chartList.get(0).getTagUserId();
		creatTable(tableName);
		for(Chat chat : chartList) {
			insertChat(tableName, chat);
		}
	}
	public void saveChat(Chat chat) {
		tableName = "chat_" + Global.user.getUserId() + "_" + chat.getTagUserId();
		creatTable(tableName);
		insertChat(tableName, chat);
	}
	/**
	 * 获取聊天内容
	 * @param user
	 * @return
	 */
	public List<Chat> getChatList(User user) {
		List<Chat> chatList = new ArrayList<Chat>();
		Chat chat;
		tableName = "chat_" + Global.user.getUserId() + "_" + user.getUserId();
		if(db.tableIsExist(tableName)) {
			Cursor mCursor = db.getCursor(tableName);
			if (mCursor != null) {
				while(mCursor.moveToNext()) {
					chat = new Chat();
					chat.setUserId(mCursor.getString(1));
					chat.setMsg(mCursor.getString(2));
					chat.setTime(mCursor.getLong(3));
					if(chat.getUserId().equals(Global.user.getUserId())) {
						repairChat(chat, Global.user);
					} else {
						repairChat(chat, user);
					}
					
					chatList.add(chat);
				}
				mCursor.close();
			}
		}
		
		return chatList;
	}
	public List<Chat> getChatList(User user, boolean all) {
		List<Chat> chatList = new ArrayList<Chat>();
		Chat chat;
		tableName = "chat_" + Global.user.getUserId() + "_" + user.getUserId();
		if(db.tableIsExist(tableName)) {
			Cursor mCursor = db.getCursor(tableName);
			if (mCursor != null) {
				int count = mCursor.getCount();
				if(!all && mCursor.getCount() > HISTORYCHATS_NUM) {
					mCursor.moveToPosition(count - HISTORYCHATS_NUM - 1);
				}
				while(mCursor.moveToNext()) {
					chat = new Chat();
					chat.setUserId(mCursor.getString(1));
					chat.setMsg(mCursor.getString(2));
					chat.setTime(mCursor.getLong(3));
					if(chat.getUserId().equals(Global.user.getUserId())) {
						repairChat(chat, Global.user);
					} else {
						repairChat(chat, user);
					}
					
					chatList.add(chat);
				}
				mCursor.close();
			}
		}
		
		return chatList;
	}
	public void deleteChat(String userId) {
		tableName = "chat_" + Global.user.getUserId() + "_" + userId;
		if(db.tableIsExist(tableName)) {
			db.dropTable(tableName);
		}
	}
	//修复Chat
	private void repairChat(Chat chat, User user) {
		chat.setUserName(user.getUserName());
		chat.setRealName(user.getRealName());
		chat.setPotraitURL(user.getImgUrl());
	}
	//保存Chat
	private void insertChat(String tableName, Chat chat) {
		ContentValues values = new ContentValues();
		values.put("userid", chat.getUserId());
		values.put("msg", chat.getMsg());
		values.put("time", chat.getTime());
		db.insert(tableName, values);
	}
	//建表
	private void creatTable(String tableName) {
		TABLE_CHAT_SQL = "create table " + tableName + " (_id integer primary key autoincrement, "
				+ "userid text not null, msg text not null, time long not null);";
		if(!db.tableIsExist(tableName)) {
			db.creatTable(TABLE_CHAT_SQL);
		}
	}
}
